/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-06-01
 * Time: 11:41
 */

public class Solution {
    public String addBinary(String a, String b) {
        StringBuilder stringBuilder = new StringBuilder();
        // 取长度最长的, 超过范围的直接取 0
        int len = Math.max(a.length(), b.length());
        int carry = 0; // 进位
        for (int i = 0;i < len;i++) {
            // 下标要从后往前
            // 如果已经超过范围了, 那么直接就是 0
            carry += i < a.length() ? (a.charAt(a.length() - i - 1) - '0') : 0;
            carry += i < b.length() ? (b.charAt(b.length() - i - 1) - '0') : 0;
            stringBuilder.append(carry % 2);
            carry /= 2;
        }
        // 最后的进位不要忘了
        if (carry == 1) {
            stringBuilder.append(1);
        }
        // 最后需要反转一下
        return stringBuilder.reverse().toString();
    }
}
